<?php
require_once 'membersdao.php';
require_once '../dto/memberdto.php';
require_once '../dto/connectiondto.php';
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of connectiondao
 *
 * @author Circle
 */
class connectiondao {
//put your code here
    public static function getFriendFromConnectionByID($id)
    {
        $sql = "SELECT MemberID, ConnectdUserID, isFriend FROM connection WHERE MemberID ='" .$id."'";
 
        $result = dataprovider::ExecuteQuery($sql);
        $arrMembers = new ArrayObject();
        while($row = mysql_fetch_array($result))
        {
            $connection = new connectiondto();
            $connection->setConnectedUserID($row['ConnectdUserID']);
            $member = membersdao::GetMembersByID($connection->getConnectedUserID());
            $arrMembers[] = $member;
        }
        return $arrMembers;
    }
      public static function getConnectFromConnectionByID($id)
    {
        $sql = "SELECT MemberID, ConnectdUserID, isFriend FROM connection WHERE MemberID ='" .$id."'";
        $result = dataprovider::ExecuteQuery($sql);
        $arrConnect = new ArrayObject();
        while($row = mysql_fetch_array($result))
        {
            $connection = new connectiondto();
            $connection->setMemberID($row['MemberID']);
            $connection->setConnectedUserID($row['ConnectdUserID']);
            $connection->setIsFriend($row['isFriend']);
            $arrConnect[] = $connection;
        }
        return $arrConnect;
    }
    
    public static function getUserNoConnection($id)
    {
        $sql = "SELECT UserID FROM members" . 
                " WHERE UserID!= '".$id."' and UserID NOT IN (
                    SELECT ConnectdUserID FROM connection WHERE MemberID = '".$id."')";
        //echo $sql;
        $result = dataprovider::ExecuteQuery($sql);
        $arrMember = new ArrayObject();
        while($row = mysql_fetch_array($result))
        {
            $member = membersdao::GetMembersByID($row['UserID']);
            $arrMember[] = $member;
        }
        return $arrMember;
    }

    public static function addFriend(connectiondto $con)
    {
        $sql = "INSERT INTO connection (MemberID, ConnectdUserID, isFriend, Members_UserID, Members_UserID1)" .
                " VALUES ('".$con->getMemberID()."','".$con->getConnectedUserID()."','".$con->getIsFriend()."', '".$con->getMemberID()."', '".$con->getConnectedUserID()."')";
        $result = dataprovider::ExecuteQuery($sql);
        if($result)
            return TRUE;
        else {
            return FALSE;
        }
    }
}

?>
